package com.java.myapp.daoImpl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.ResourceBundle;

import com.java.myapp.dao.UserDao;
import com.java.myapp.entity.UserEntity;
import com.java.myapp.utility.ResourceService;

public class UserDaoImpl implements UserDao {
	String url=null;
	String user=null;
	String password=null;
	String databaseName=null;
	private static Connection con;
	{
		ResourceBundle rb=ResourceService.getResourceBundle();
		url=new String((String)rb.getObject("url"));
		user=new String(rb.getString("userName"));
		password=new String(rb.getString("password"));
		databaseName=new String((String)rb.getObject("databaseName"));
	}
	private UserDaoImpl(){
		Connection con = null;
		try{
			con=DriverManager.getConnection(url+databaseName, user, password);
			UserDaoImpl.con=con;
		}catch(SQLException e){
			System.out.format("Error: %1$s%nFrom Class: %2$s %n", e.getMessage(),e.getClass());
		}
	}
	

	@Override
	public Integer createUser(UserEntity userEntity) throws Exception {
		String sql="SELECT * FROM user ORDER BY userId DESC";
		ResultSet rs = null;
		try(Statement statement=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
				ResultSet.CONCUR_UPDATABLE);){
			rs=statement.executeQuery(sql);
			rs.moveToInsertRow();
			rs.updateString(2, userEntity.getUserName());
			rs.updateString(3, userEntity.getPassword());
			rs.updateObject(4, new Date());
			rs.updateString(5, null);
			rs.insertRow();
			rs.close();
			rs=statement.executeQuery(sql);
			rs.next();
			return Integer.parseInt(rs.getString(1));
		}finally{
			rs.close();
		}
		
	}

	@Override
	public Integer updateUser() throws Exception {
		return null;
	}

	@Override
	public void deleteUser() throws Exception {
		
	}

	@Override
	public UserEntity getUserByName(String userName) throws Exception {
		String sql="SELECT * FROM user WHERE userName='"+userName+"'";
		ResultSet rs=null;
		try(Statement st=con.createStatement();){
			rs=st.executeQuery(sql);
			return !rs.next()?null:new UserEntity(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getDate(4),rs.getDate(5));
		}finally{
			if(rs!=null){
				rs.close();
			}
		}
	}
	
	@Override
	public Boolean checkLogin(UserEntity user) {
		String sql="SELECT * FROM user WHERE userName='"+user.getUserName()+"' AND password='"+user.getPassword()+"'";
		ResultSet rs=null;
		try(Statement st=con.createStatement();){
			rs=st.executeQuery(sql);
			return rs.next();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}finally{
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
			
	}

	@Override
	public UserEntity getUserById(Integer id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}


	public static UserDao getUserDao() {
			return new UserDaoImpl();
	}


	public static Connection getCon() {
		if(con==null){
			new UserDaoImpl();
		}
		return con;
	}

	public static void setCon(Connection con) {
		UserDaoImpl.con = con;
	}


}
